package com.tiantianyuegou.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tiantianyuegou.R;
import com.tiantianyuegou.model.BankModel;

import java.util.List;

/**
 * Created by Administrator on 2016/1/7.
 */
public class SelectListAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;

    List<BankModel> bankModelList;

    private String type;
    private int num = -1;

    ViewHolder holder;


    public interface iDeleteListener {
        void deleteBank(int position);
    }

    private iDeleteListener mListener;

    public void setDeleteListener(iDeleteListener listener) {
        Log.e("TAG","set listener");
        mListener = listener;
    }

    public SelectListAdapter() {

    }

    public SelectListAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public SelectListAdapter(Context context, List<BankModel> bankModelList, String type) {
        this.context = context;
        this.bankModelList = bankModelList;
        this.type = type;
        inflater = LayoutInflater.from(context);
    }

    public void getData(List<BankModel> bankModelList) {
        this.bankModelList = bankModelList;
    }

    public SelectListAdapter(Context context, String type) {
        this.context = context;
        this.type = type;
        inflater = LayoutInflater.from(context);

    }

//    public void getNum(int i) {
//
//        num = i;
//    }

    @Override
    public int getCount() {
        return bankModelList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_list_select, null);
            holder = new ViewHolder();
            holder.text_name = (TextView) view.findViewById(R.id.text_bank_name);
            holder.text_id = (TextView) view.findViewById(R.id.text_bank_id);
            holder.img_bank = (ImageView) view.findViewById(R.id.img_bank);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.text_name.setText(bankModelList.get(position).getBank());
        String tbNumber = bankModelList.get(position).getTbNumber();
        String bankNume = tbNumber.substring(tbNumber.length() - 2);
        String bankNums = tbNumber.substring(0, tbNumber.length() - 5);
        holder.text_id.setText(bankNums + "●●●" + bankNume);

//        holder.img_bank.setImageResource(R.mipmap.ic_launcher);

        if (!type.isEmpty()) {
            if (type.equals("1")) {
                if (bankModelList.get(position).isSelected()) {
                    holder.img_bank.setVisibility(View.VISIBLE);
                    holder.img_bank.setImageResource(R.mipmap.select_icon_2);
                    notifyDataSetChanged();
                }
            } else if (type.equals("2")) {
                holder.img_bank.setVisibility(View.VISIBLE);
                holder.img_bank.setImageResource(R.mipmap.lajitong);
                holder.img_bank.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e("TAG","click  delete");
//                        if (mListener == null) {
                            mListener.deleteBank(position);
//                        }
                        
                    }
                });
            }
        }


        return view;
    }

    public static class ViewHolder {
        TextView text_name, text_id;
        ImageView img_bank;
    }
}
